/*
 * @Descripttion: 项目中用到的正则校验
 * @version:
 * @Author: 周子尧
 * @Date: 2021-01-27 11:21:27
 * @LastEditors: 周子尧
 * @LastEditTime: 2021-02-02 09:29:38
 */

const externalReg = /^(https?:|mailto:|tel:)/
const urlReg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
const idCardReg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
const phoneReg = /^(0|86|17951)?(13[0-9]|14[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57]|19[0-9])[0-9]{8}$/
const emailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

export default {
  externalReg,
  urlReg,
  idCardReg,
  phoneReg,
  emailReg
}

/**
 * 是否来自外部
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return externalReg.test(path)
}

/**
 * 是否是链接
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  return urlReg.test(url)
}

/**
 * 身份证校验
 * @param {*} idcard
 */
export function isIDCard(idcard) {
  return idCardReg.test(idcard)
}

/**
 * 是否手机号
 * @param {*} phone
 */
export function isPhone(phone) {
  return phoneReg.test(phone)
}

/**
 * 邮箱校验
 * @param {string} email
 * @returns {Boolean}
 */
export function isEmail(email) {
  return emailReg.test(email)
}
